<?php

class mobileapi_rpc_erpapi {

    function __construct($app) {
        $this->app = $app;
    }

    public function get_list() {
        $order_id = $_POST['order_id'];
        $user_name = $_POST['user_name'];
        $goods_name = $_POST['goods_name'];
        $order_start = $_POST['order_start'];
        $order_end = $_POST['order_end'];

        $where_and = '';
        if (!empty($order_id)) {
            $where_and .= " AND sdb_b2c_orders.order_id={$order_id}";
        }
        if (!empty($user_name)) {
            $where_and .= " AND sdb_b2c_members.name LIKE '%{$user_name}%'";
        }
        if (!empty($order_start) && !empty($order_end)) {
            if ($order_start > $order_end) {
                return '开始时间不能大于结束时间';
            }
            $where_and .= " AND sdb_b2c_orders.createtime>={$order_start} AND sdb_b2c_orders.createtime<{$order_end}";
        }

        if (!empty($goods_name)) {
            $select_goods_name = "LEFT JOIN (SELECT order_id FROM sdb_b2c_order_items WHERE `name` LIKE '%{$goods_name}%') og ON og.order_id=sdb_b2c_orders.order_id";
        } else {
            $select_goods_name = '';
        }

        $sql = "SELECT sdb_b2c_orders.order_id,sdb_b2c_orders.pay_status,sdb_b2c_orders.ship_status,sdb_b2c_orders.createtime,
          sdb_b2c_orders.ship_name,sdb_b2c_orders.ship_addr,sdb_b2c_orders.payed,sdb_b2c_orders.payment,
          sdb_b2c_orders.discount+sdb_b2c_orders.pmt_goods order_promo,sdb_b2c_orders.score_u point_promo,
          sdb_b2c_orders.pmt_order coupon_promo,sdb_b2c_orders.memo message,
          sdb_b2c_members.source,sdb_b2c_members.source_app,sdb_b2c_delivery.t_begin shipping_begin
          FROM sdb_b2c_orders
          LEFT JOIN sdb_b2c_members ON sdb_b2c_orders.member_id=sdb_b2c_members.member_id
          LEFT JOIN sdb_b2c_delivery ON sdb_b2c_delivery.order_id=sdb_b2c_orders.order_id
          {$select_goods_name}
          WHERE pay_status in('1','2') {$where_and}";

        $order_data = kernel::database()->select($sql);

        $order_id_list = array();
        $order_id_map = array();
        $obj_payments_cfgs = app::get('ectools')->model('payment_cfgs');
        foreach ($order_data as $key => &$row) {
            $order_id_list[] = $row['order_id'];
            $order_id_map[$row['order_id']] = $key;
            $order_data[$key]['goods_list'] = array();
            if ($row['source'] == 'api') {
                if ($row['source_app'] != '') {
                    $row['source'] = 'android';
                } else {
                    $row['source'] = 'ios';
                }
            }
            $row['payment'] = $obj_payments_cfgs->get_app_display_name($row['payment']);
        }
        $order_ids = '\'' . join('\',\'', $order_id_list) . '\'';
        $n_sql = "SELECT `name`,order_id FROM sdb_b2c_order_items WHERE order_id in({$order_ids})";
        $order_goods_names = kernel::database()->select($n_sql);

        foreach ($order_goods_names as $row) {
            $order_data[$order_id_map[$row['order_id']]]['goods_list'][] = $row['name'];
        }

        return $order_data;
    }

}